<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog v-model="props.dialogVisible" :title="props.pageType == 'EDIT' ? '编辑算法' : (props.pageType == 'ADD' ? '新增算法' : '算法详情')" width="1000px" @close="cancel">
    <div class="algorithm-main">
      <div class="code-info">
        <CodeEditor 
          :height="800"
          :width="600"
          :code="implementation"
          :disabled="props.pageType == 'VIEW'"
          class="editor"
          @transferCode="getCode"
        />
      </div>
      <div class="algorithm-info">
        <el-form ref="tempFormRef" size="small" :model="codeInfo" label-width="120px">
          <el-form-item label="算法名称" prop="algorithmName">
            <el-input :disabled="props.pageType == 'VIEW'" v-model="codeInfo.algorithmName" placeholder="请输入算法名称" clearable />
          </el-form-item>
          <el-form-item label="实现语言" prop="algorithmLanguage">
            <el-select :disabled="props.pageType == 'VIEW'" v-model="codeInfo.algorithmLanguage" placeholder="请选择实现语言">
              <el-option v-for="item in props.languageList" :label="item.name" :value="item.val" :key="item.val"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="算法业务类型" prop="algorithmType">
            <el-select :disabled="props.pageType == 'VIEW'" v-model="codeInfo.algorithmType" placeholder="请选择算法业务类型">
              <el-option label="型材算法" value="section"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="是否默认算法" prop="isDefault">
            <el-select v-model="codeInfo.isDefault" placeholder="请选择是否默认算法">
              <el-option label="默认" :value="0"></el-option>
              <el-option label="item.name" :value="1"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="算法描述" prop="algorithmName">
            <el-input :disabled="props.pageType == 'VIEW'" v-model="codeInfo.description" type="textarea" rows="3" placeholder="请输入算法描述" clearable />
          </el-form-item>
          <el-form-item label="入参格式" prop="param">
            <el-input :disabled="props.pageType == 'VIEW'" v-model="codeInfo.param" type="textarea" rows="5" placeholder="请输入入参格式" clearable />
          </el-form-item>
        </el-form>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="cancel"> 取消 </el-button>
        <el-button v-if="props.pageType !== 'VIEW'" size="small" type="primary" @click="save"> 保存 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script name="AlgorithmOperate" setup>
import CodeEditor from "@/components/CodeEditor";
import { addAlgorithmConfig, updateAlgorithmConfig, getAlgorithmConfigDetail } from '@/api/algorithmConfig/index';
const props = defineProps(['dialogVisible', 'row', 'pageType', 'languageList']);
const emit = defineEmits(['handleEvent']);
const tempFormRef = ref();
const codeHeight = ref('calc(70vh - 50px)');
const codeInfo = ref({
  implementation: ''
});
const implementation = ref('');
// 取消
const cancel = () => {
  emit('handleEvent', {type: 'cancel'});
};
const getCode = (code) => {
  implementation.value = code;
}
// 保存
const save = () => {
  if (!implementation.value) {
    ElMessage({ showClose: true, message: `请先输入算法！`, type: 'warning' });
    return;
  }
  tempFormRef.value.validate((valid) => {
    if (valid) {
      let param = {
        id: props?.row?.id,
        ...codeInfo.value
      }
      props.pageType == 'ADD' && delete param.id;
      param.implementation = implementation.value;
      let apiInterface = props.pageType == 'ADD' ? addAlgorithmConfig : updateAlgorithmConfig;
      apiInterface(param).then((res) => {
        if (res.code === 200) {      
          ElMessage({ showClose: true, message: `${props.pageType == 'ADD' ? '新增成功！' : '编辑成功！'}`, type: 'success' });
          emit('handleEvent', {type: 'success'});
        } else {
          ElMessage({ showClose: true, message: `${props.pageType == 'ADD' ? '新增失败！' : '编辑失败！'}`, type: 'warning' });
        }
      });
    }
  })
}     
// 获取详情
const getDetail = (id) => {
  getAlgorithmConfigDetail(id).then(res => {
    if (res) {
      codeInfo.value = res.data;
      implementation.value = res.data.implementation;
    }
  })
}
onMounted(async () => {
  props?.row?.id && getDetail(props?.row?.id);
});
</script>
<style lang="scss" scoped>
.algorithm-main {
  display: flex;
  .code-info {
    flex:0 0 600px;
    // min-height: 400px;
    height: 70vh;
    overflow-y: scroll;
    overflow-x: hidden;
    .editor {
      width: 600px;
      min-height: 400px;
    }
  }
  .algorithm-info {
    flex: 1;
    // height: 800px; 
    // border: 1px solid #dcdfe6;
  }  
}
</style>
